﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace TS_XY_Train_Repaire.ToolSystem
{
    public partial class Tool_System_GroupList_Form : DevExpress.XtraEditors.XtraForm
    {
        DataTable m_dtToolType = new DataTable();
        int m_iGroupID = 0;

        public Tool_System_GroupList_Form()
        {
            InitializeComponent();
        }

        private void Tool_System_GroupList_Form_Load(object sender, EventArgs e)
        {
            _LoadData();

            _LoadMatchData();
        }

        private void _LoadData()
        {
            try
            {
                DataTable dt = ToolDataMgr.GetInstance().GetAllGroup();
                gc_group.DataSource = dt;
                if (dt.Rows.Count > 0)
                {
                    m_iGroupID = int.Parse(dt.Rows[0]["f_id"].ToString());
                }

                m_dtToolType = ToolDataMgr.GetInstance().GetToolTypeDataTable();
                m_dtToolType.Columns.Add("f_check", typeof(bool));
            }
            catch
            {
                gc_group.DataSource = null;
                gc_group.Refresh();
            }
        }

        private void _LoadMatchData()
        {
            try
            {
                if (m_iGroupID >= 0)
                {
                    HashSet<int> setToolType = new HashSet<int>();

                    DataTable dt = ToolDataMgr.GetInstance().GetGroupMatchDataTable(m_iGroupID);
                    foreach (DataRow dataRow in dt.Rows)
                    {
                        int iToolType = int.Parse(dataRow["f_tooltypeid"].ToString());
                        setToolType.Add(iToolType);
                    }

                    foreach (DataRow dataRow in m_dtToolType.Rows)
                    {
                        int iToolType = int.Parse(dataRow["f_id"].ToString());
                        
                        if (setToolType.Contains(iToolType))
                        {
                            dataRow["f_check"] = true;
                        }
                        else
                        {
                            dataRow["f_check"] = false;
                        }
                    }
                    gc_tool.DataSource = m_dtToolType;
                }

                return;
            }
            catch
            {
                
            }

            gc_tool.DataSource = null;
            gc_tool.Refresh();
        }

        private void gv_group_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            DataRow dataRow = gv_group.GetDataRow(e.FocusedRowHandle);
            if (dataRow != null)
            {
                m_iGroupID = int.Parse(dataRow["f_id"].ToString());
                _LoadMatchData();
            }
        }

        private void gv_tool_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            DataRow dataRow = gv_tool.GetFocusedDataRow();
            if (dataRow != null)
            {
                int iToolType = int.Parse(dataRow["f_id"].ToString());
                bool bCheck = bool.Parse(dataRow["f_check"].ToString());

                if (bCheck)
                {
                    ToolDataMgr.GetInstance().AddGroupMatch(m_iGroupID, iToolType);
                }
                else
                {
                    ToolDataMgr.GetInstance().DelGroupMatch(m_iGroupID, iToolType);
                }
            }
        }
    }
}